package com.cofi.vueelementadminserver.config.security;

import com.alibaba.fastjson2.JSON;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import java.io.IOException;
import java.util.HashMap;

/**
 * @author xyh
 * @date 2024-12-30
 * @description 认证失败处理
 */
public class MyLoginFailHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        String name = request.getParameter("username");
        String pwd = request.getParameter("password");
        HashMap<String, Object> result = new HashMap<>();
        result.put("status",200);
        result.put("code",10001);
        result.put("message","登录失败" );
        result.put("description", exception.getMessage());

        String resultJson = JSON.toJSONString(result);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().println(resultJson);
    }
}
